var app = getApp();
//获取应用实例
Page({
  data: {
    userInfo: null,
    hideFieldState: true, //是否隐藏钓场管理
    showUserModal: false,
  },
  onLoad: function() {

  },
  onShow: function() {
    var that = this;
 
    if (that.data.userInfo == null) {
      app.getUserInfo(function(res) {
        if (res == null) {
          that.setData({
            showUserModal: true
          })
        } else {
          that.setData({
            userInfo: res
          })
          app.checktoken(function () {
            that.getFieldCount();
          });
          console.log(res);
        }
      });
    } else {
     
      app.checktoken(function() {
        that.getFieldCount();
      });
    }
  },
  /**
   * 隐藏模态对话框
   */
  hideUserModal: function() {
    this.setData({
      showUserModal: false
    });
  },
  getFieldCount: function(id) {
    wx.showLoading({
      title: '正在加载信息',
    })
    var that = this;
    var _token = app.globalData.token;
    wx.request({
      url: app.globalData.Domain + '/field/mycount',
      data: {
        token: _token
      },
      success: function(res) {
        if (res.data.code == 0) {
          if (res.data.data > 0) {
            that.setData({
              hideFieldState: false
            });
          }
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },
  onGotUserInfo: function(res) {
    var that = this;
    that.hideUserModal();
    console.log(res);
    if (res.detail.userInfo == null) {
      wx.showModal({
        title: '哪有鱼提示',
        content: '我很有用的，请不要拒绝我哦，再来一次吧',
        showCancel: false,
        success: function(res) {
          that.setData({
            showUserModal: true
          });
        }
      })
    } else {
      app.globalData.userInfo = res.detail.userInfo;
      that.setData({
        userInfo: res.detail.userInfo
      })
      //登录获取token
      app.login(function() {
        that.getFieldCount();
      });
    }

  },
});